import Mock from "mockjs";
// 假数据添加
var ly = Mock.mock({
  "data|40": [
    {
      // Id
      "userId|+1": 1,
      // 姓名
      name: "@cname",
      // 评论
      comment: "@cword(30)",
      // 日期
      date: /^(2022-02-1|2022-02-2)\d{1}$/,
      // 手机号
      tel: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
      checked: 0,
    },
  ],
});
class delMsg {
  // 获取留言
  getData(current, pagesize) {
    return Promise.resolve({
      count: ly.data.length,
      ly: ly.data.slice((current - 1) * pagesize, current * pagesize),
    });
  }
  // 添加留言
  add(msg) {
    ly.data.unshift(msg);
    console.log(msg);
  }
  // 删除选中的单条消息
  delete(userId) {
    ly.data = ly.data.filter((item) => item.userId !== userId);
    return Promise.resolve(true);
  }
  // 查找留言
  // val表示input里面的内容
  // current表示当前页
  // pagesize表示美业的信息数
  find(val, current, pagesize) {
    // 设置一个空数组，用来储存符合搜索内容的留言
    let arr = [];
    if (val != "") {
      ly.data.map((item) => {
        for (const key in item) {
          if (val === item[key]) {
            arr.push(item);
          }
        }
      });
    } else {
      arr = [...ly.data];
    }
    // console.log(arr);
    return Promise.resolve({
      count: arr.length,
      ly: arr.slice((current - 1) * pagesize, current * pagesize),
    });
  }
  checkAll(i, current, pagesize) {
    ly.data.map((item) => {
      item.checked = i;
    });
    return Promise.resolve({
      count: ly.data.length,
      ly: ly.data.slice((current - 1) * pagesize, current * pagesize),
    });
  }
  itemCheck1(id, checked, current, pagesize) {
    let arr = [];
    ly.data.map((item) => {
      if (item.userId === id) {
        item.checked = !item.checked;
      }
      if (item.checked == 1) {
        arr.push(item);
      }
    });
    if (arr.length === ly.data.length) {
      checked = 1;
    } else {
      checked = 0;
    }
    //点击重新渲染
    change.getData(current, pagesize);
    return checked;
  }

  // 删除选中
  delchoose(current, pagesize) {
    // 删除选中的多条
    let data3 = ly.data.filter((item) => {
      return item.checked == 0
    });
    ly.data = [...data3];
    return Promise.resolve({
      count: ly.data.length,
      ly: ly.data.slice((current - 1) * pagesize, current * pagesize),
    });
  }
}
const change = new delMsg();

export default change;
